package com.cloud.DrugManagement.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.DrugManagement.enums.OrderState;
import com.cloud.DrugManagement.mapper.OrderDetailsMapper;
import com.cloud.DrugManagement.mapper.ReceiptMonocularMapper;
import com.cloud.DrugManagement.pojo.OrderDetails;
import com.cloud.DrugManagement.pojo.ReceiptMonocular;
import com.cloud.DrugManagement.service.ReceiptMonocularService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhozixiong
 * @date 2023/02/17
 */
@Service
@RequiredArgsConstructor
public class ReceiptMonocularServiceImpl extends ServiceImpl<ReceiptMonocularMapper, ReceiptMonocular> implements ReceiptMonocularService {

    private final int SIZE = 10;

    private final OrderDetailsMapper detailsMapper;

    @Override
    public Page<ReceiptMonocular> searchReceipt(int pages, long oid) {
        Page page = new Page(pages, SIZE);
        OrderDetails orderDetails = detailsMapper.searchAllByOid(oid);
        if(orderDetails.getState().equals(OrderState.DELIVERED) && orderDetails.getState().equals(OrderState.COMPLETE)) {
            return baseMapper.searchSubmit(page, oid);
        }else {
            return baseMapper.searchNotSubmit(page, oid);
        }
    }

    @Override
    public boolean updateReceiptMonocular(long oid, int did, int quantity) {
        ReceiptMonocular receiptMonocular = baseMapper.searchAllByOidAndDid(oid, did);
        if(receiptMonocular != null) {
            return baseMapper.updateQuantityAndMoneyByOidAndDid(quantity, oid, did) >= 1;
        }else {
            return baseMapper.insert(oid, did, quantity) >= 1;
        }
    }

    @Override
    public double searchTotalAmount(long oid) {
        return baseMapper.searchTotalAmount(oid);
    }
}
